<?php
declare (strict_types=1);

namespace app\admin\model\iot;

use app\admin\model\base\User;
use app\BaseModel;

/**
 * 今日出袋记录表模型
 */
class ScanBagDay extends BaseModel
{

    /**
     * 每天取袋次数限制
     * @var int
     */
    private static int $DailyLimit = 3;

    /**
     * 通过用户ID获取取袋次数
     * @param int $user_id
     * @return array
     */
    public static function getUserIdDayNum(int $user_id): array
    {
        $DailyLimit = self::$DailyLimit;
        //处理特殊情况次数限制
        $data['daily_limit'] = 0;
        $data['day_used'] = 0;
        $data['day_surplus'] = 0;
        //获取用户信息
        $user_info = User::getIdUserInfo($user_id);
        if (!$user_info) {
            return $data;
        }
        $user_except = explode(",",env('scan_bag.user_except'));
        if (in_array($user_info['id'], $user_except)) {
            $DailyLimit = 100;
        }
        //查询今天免费取袋次数
        $where = ['user_id' => $user_id, 'day' => date('Y-m-d')];
        $day_num = self::where($where)->count();
        //剩余次数
        $day_surplus = max(($DailyLimit - $day_num), 0);
        //返回参数
        $data['daily_limit'] = $DailyLimit;
        $data['day_used'] = $day_num;
        $data['day_surplus'] = $day_surplus;
        return $data;
    }

    /**
     * 写入用户当日取代次数
     * @param int $user_id
     * @return ScanBagDay|\think\Model
     */
    public static function writeUserIdDayNum(int $user_id): ScanBagDay|\think\Model
    {
        $data = ['user_id' => $user_id, 'day' => date('Y-m-d')];
        return self::create($data);
    }

    //模型事件


}




